<script setup>
import { getOrderAPI, getUserInfo, payByBalanceAPI } from '@/api/pay';
import { onMounted, ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { useCountDown } from '@/composables/countDown';
import { ElButton, ElMessage } from 'element-plus';
const route = useRoute()
const router = useRouter()
const payInfo = ref({})
const getOrderInfo = async () => {
  const { result } = await getOrderAPI(route.query.id)
  // console.log(result)
  payInfo.value = result
  start(result.countdown)
}
onMounted(() => {
  getOrderInfo()
})
const { formatTime1, start } = useCountDown()
const paying = ref(false)
const payByBalance = async (money) => {
  paying.value = true
  const { result } = await getUserInfo()
  if (result.balance >= money) {
    await payByBalanceAPI(payInfo.value.id)
    ElMessage.success('支付成功')
    router.push({
      path: '/paycallback',
      query: { 
        id: route.query.id,
        payResult: true
       }
    })
  } else {
    ElMessage.error('您的账户余额不足，请充值')
    router.push({
      path: '/paycallback',
      query: { 
        id: payInfo.id,
        payResult: false
       }
    })
  }
  paying.value = false
}
</script>


<template>
  <div class="xtx-pay-page">
    <div class="container">
      <!-- 付款信息 -->
      <div class="pay-info">
        <span class="icon iconfont icon-queren2"></span>
        <div class="tip" v-if="payInfo.countdown >= 0">
          <p>订单提交成功！请尽快完成支付。</p>
          <p>支付还剩 <span>{{ formatTime1 }}</span>, 超时后将取消订单</p>
        </div>
        <div class="amount">
          <span>应付总额：</span>
          <span>¥{{ payInfo.payMoney }}</span>
        </div>
      </div>
      <!-- 付款方式 -->
      <div class="pay-type">
        <p class="head">选择以下支付方式付款</p>
        <div class="item">
          <p>支付平台</p>
          <a class="btn wx" href="javascript:;"></a>
          <button class="btn" @click="payByBalance(payInfo.payMoney)">余额支付</button>
          <a class="btn alipay" :href="payUrl"></a>
        </div>
        <div class="item">
          <p>支付方式</p>
          <a class="btn" href="javascript:;">招商银行</a>
          <a class="btn" href="javascript:;">工商银行</a>
          <a class="btn" href="javascript:;">建设银行</a>
          <a class="btn" href="javascript:;">农业银行</a>
          <a class="btn" href="javascript:;">交通银行</a>
        </div>
      </div>
    </div>
    <div v-if="payInfo.countdown < 0" class="add-box">
      <div style="margin: auto;">订单超时，请重新下单！</div>
      <el-button @click="$router.push('/')" style="margin: auto;">返回首页</el-button>
    </div>
    <div class="overlay" v-if="payInfo.countdown < 0 || paying"></div>

  </div>
</template>

<style scoped lang="scss">
.xtx-pay-page {
  margin-top: 20px;
}

.overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5) url(../../assets/images/loading.gif) no-repeat center;
  /* 半透明黑色背景 */
  z-index: 999;
  /* 确保遮罩层在其他元素之上 */
}

.pay {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  border: 1px solid #ccc;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  padding: 20px;
  width: 600px;
  height: 600px;
  z-index: 1001;
}

.add-box {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  border: 1px solid #ccc;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  padding: 20px;
  width: 200px;
  height: 200px;
  z-index: 1001;

  .inp {
    width: 70%;
    margin-left: 5%;
  }

  .add-box-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 5%;
    background-color: #f5f5f5;
    border-top-left-radius: 8px;
    border-top-right-radius: 8px;
  }

  .add-box-title {
    margin: 0;
    font-size: 18px;
    font-weight: bold;
  }

  .close-icon {
    cursor: pointer;
    font-size: 20px;
    font-weight: bold;
  }

}

.pay-info {

  background: #fff;
  display: flex;
  align-items: center;
  height: 240px;
  padding: 0 80px;

  .icon {
    font-size: 80px;
    color: #1dc779;
  }

  .tip {
    padding-left: 10px;
    flex: 1;

    p {
      &:first-child {
        font-size: 20px;
        margin-bottom: 5px;
      }

      &:last-child {
        color: #999;
        font-size: 16px;
      }
    }
  }

  .amount {
    span {
      &:first-child {
        font-size: 16px;
        color: #999;
      }

      &:last-child {
        color: $priceColor;
        font-size: 20px;
      }
    }
  }
}

.pay-type {
  margin-top: 20px;
  background-color: #fff;
  padding-bottom: 70px;

  p {
    line-height: 70px;
    height: 70px;
    padding-left: 30px;
    font-size: 16px;

    &.head {
      border-bottom: 1px solid #f5f5f5;
    }
  }

  .btn {
    width: 150px;
    height: 50px;
    border: 1px solid #e4e4e4;
    text-align: center;
    line-height: 48px;
    margin-left: 30px;
    color: #666666;
    display: inline-block;

    &.active,
    &:hover {
      border-color: $xtxColor;
    }

    &.alipay {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/7b6b02396368c9314528c0bbd85a2e06.png) no-repeat center / contain;
    }

    &.wx {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/c66f98cff8649bd5ba722c2e8067c6ca.jpg) no-repeat center / contain;
    }
  }
}
</style>